#!/usr/bin/env python
import pygame
from math import *

class Foot:
    def __init__(self, xoff, yoff, image, index):
        self.image = image
        self.image_backup = image.copy()
        self.xoff = xoff
        self.yoff = yoff
        self.xadd = 0
        self.yadd = 0
        self.angle = 0
        self.index = index
        self.direction = 0
        self.reset()
    
    def walk(self, speed):
        self.set_angle(0)
        speed = fabs(speed)
        addPos = float(speed) / 2.5
        if self.direction == 180:
            addPos *= -1
        
        if self.path == 1:
            self.pathPos += addPos
            self.xadd = self.pathPos
            self.yadd = - self.calcPathY(self.pathPos)
            
            if self.direction == 0:
                if self.pathPos + addPos > 20:
                    self.path = 2
                    self.pathPos = 20
            else:
                if self.pathPos + addPos < 0:
                    self.path = 2
                    self.pathPos = 0
            
        else:
            self.pathPos -= addPos
            self.xadd = self.pathPos
            self.yadd = 0
            
            if self.direction == 0:
                if self.pathPos - addPos < 0:
                    self.path = 1
            else:
                if self.pathPos - addPos > 20:
                    self.path = 1
        
    def stopWalking(self):
        self.reset()
        self.set_angle(0)
    
    def jump(self, vspeed):
        self.reset()
        if self.direction == 0:
            if self.angle > -80:
                self.set_angle(self.angle - fabs(vspeed))
            else:
                self.set_angle(-80)
        else:
            if self.angle < 80:
                self.set_angle(self.angle + fabs(vspeed))
            else:
                self.set_angle(80)
    def fall(self, vspeed):
        self.reset()
        if self.direction == 0:
            if self.angle < 20:
                self.set_angle(self.angle + fabs(float(vspeed)))
            else:
                self.set_angle(20)
        else:
            if self.angle > -20:
                self.set_angle(self.angle - fabs(float(vspeed)))
            else:
                self.set_angle(-20)
        
    def reset(self):
        if self.index == 1:
            self.pathPos = 0
        else:
            self.pathPos = 20
        self.path = 1
        self.xadd = self.pathPos
        self.yadd = -self.calcPathY(self.pathPos)
    
    def set_angle(self, angle):
        if self.angle != angle:
            self.image = pygame.transform.rotate(self.image_backup, angle)
            self.angle = angle
    
    def set_direction(self, direction):
        if self.direction != direction:
            self.image = pygame.transform.flip(self.image, 1, 0)
            self.image_backup = pygame.transform.flip(self.image_backup, 1, 0)
            self.set_angle(-self.angle)
            self.direction = direction
     
    def calcPathY(self, x):
        return 4 * pi * sin(float(1) / (2 * pi) * x)
        
    def draw(self, camera, parent):
        camera.blit(self.image, (parent.x + self.xoff + self.xadd, parent.y + self.yoff + self.yadd))
    